<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
defined('WEB_ROOT') or define('WEB_ROOT', dirname(__DIR__) . '/api/web'); //web目录的路径，用户访问的静态文件都放这里
require(__DIR__ . '/vendor/autoload.php');

$config = [
    'id' => 'api-api',
    'setting' => [
        // swoole_server 的配置。
        // @see 其他配置项见 https://wiki.swoole.com/wiki/page/274.html
        'daemonize'=> 0,
        'worker_num'=> 1,
        'task_worker_num' => 1,
        'log_file' => __DIR__.'/api/runtime/logs/swoole.log',
        'log_level'=> 0,
        'chroot' => '/',
    ],
    'cacheTable' => function (){
        // swoole_table 需要提前启动，大小为 2 的次方
        return deepziyu\yii\swoole\cache\SwooleCache::initCacheTable(1024);
    },
    'bootstrap' => [
        'class' => 'deepziyu\yii\swoole\bootstrap\YiiWeb',
        'config' => function(){
            // 用闭包是为了延迟加载
            // 返回 Yii 的各个组件的配置
            require_once(__DIR__ . '/vendor/autoload.php');
            require_once(__DIR__ . '/vendor/deepziyu/yii2-swoole/Yii.php');
            require(__DIR__ . '/common/config/bootstrap.php');
            
            $config = yii\helpers\ArrayHelper::merge(
                require(__DIR__ . '/common/config/main.php'),
                require(__DIR__ . '/common/config/main-local.php'),
                require(__DIR__ . '/api/config/main.php'),
                require(__DIR__ . '/api/config/main-local.php'),
                [
                    'components' => [
                        'errorHandler' => [
                            'class'=>'deepziyu\yii\swoole\web\ErrorHandler'
                        ],
                        'cache' => [
                            'class' => 'deepziyu\yii\swoole\cache\SwooleCache',
                        ],
                    ],
                ]
            );
            
            return $config;
        },
    ],

];

// 配置了这么多，最终还是逃不过一 run()
deepziyu\yii\swoole\server\Server::run($config);
